﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;

namespace WebSite.admin.order
{
    public partial class orderlist : BaseAdmin
    {

        // Methods
        private void Bind()
        {
            if ((base.Request.QueryString["mid"] != null) && (base.Request.QueryString["action"] != null))
            {
                if (base.Request.QueryString["action"].ToString() == "del")
                {
                    SQLAccess.DoSql("delete from DB_order where orderid=" + base.Request.QueryString["mid"].ToString() + "");
                    SQLAccess.DoSql("delete from DB_Car where orderid=" + base.Request.QueryString["mid"].ToString() + "");
                }
                base.Response.Write("<script language='javascript'>window.location.href='order.aspx';</script>");
                base.Response.End();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.GridViewBind("");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {
                CheckBox box = new CheckBox();
                box = (CheckBox)e.Row.FindControl("CheckBox1");
                if (box != null)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        e.Row.Cells[1].Width = 80;
                        DataTable table = SQLAccess.GreatDs("select payauthor from DB_Pay where payid=" + e.Row.Cells[4].Text + "").Tables[0];
                        if (table.Rows.Count > 0)
                        {
                            e.Row.Cells[4].Text = table.Rows[0][0].ToString();
                        }
                        e.Row.Cells[5].Text = info.info1("", "shophuobi") + string.Format("{0:f}", Convert.ToSingle(e.Row.Cells[5].Text)) + " 元";
                        if (e.Row.Cells[7].Text.ToString() == "0")
                        {
                            e.Row.Cells[7].Text = "新订单";
                        }
                        if (e.Row.Cells[7].Text.ToString() == "1")
                        {
                            e.Row.Cells[7].Text = "备货中";
                        }
                        if (e.Row.Cells[7].Text.ToString() == "2")
                        {
                            e.Row.Cells[7].Text = "已发货";
                        }
                        if (e.Row.Cells[7].Text.ToString() == "3")
                        {
                            e.Row.Cells[7].Text = "已完成";
                        }
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");
                    }
                    else if (e.Row.RowType == DataControlRowType.Header)
                    {
                        box.Attributes.Add("onclick", "SelectAll(this.checked)");
                    }
                    e.Row.Cells[8].Visible = false;
                }
            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sqlsort = "";
            sqlsort = "select * from DB_order where languages=" + info.languages("") + " ";
            if (base.Request.Form["ordercode"] != "")
            {
                sqlsort = sqlsort + " and ordercode='" + base.Request.Form["ordercode"] + "' ";
            }
            if (base.Request.Form["states"] != null)
            {
                sqlsort = sqlsort + " and states=" + base.Request.Form["states"] + " ";
            }
            if (base.Request.Form["cname"] != "")
            {
                sqlsort = sqlsort + " and cname='" + base.Request.Form["cname"] + "' ";
            }
            if (base.Request.Form["cname1"] != "")
            {
                sqlsort = sqlsort + " and cname1='" + base.Request.Form["cname1"] + "' ";
            }
            if (base.Request.Form["address"] != "")
            {
                sqlsort = sqlsort + " and address1='" + base.Request.Form["address"] + "' ";
            }
            if (base.Request.Form["post"] != "")
            {
                sqlsort = sqlsort + " and post1='" + base.Request.Form["post"] + "' ";
            }
            if (base.Request.Form["tel"] != "")
            {
                sqlsort = sqlsort + " and tel1='" + base.Request.Form["tel"] + "' ";
            }
            if (base.Request.Form["phone"] != "")
            {
                sqlsort = sqlsort + " and phone1='" + base.Request.Form["phone"] + "' ";
            }
            if (base.Request.Form["ship"] != "0")
            {
                sqlsort = sqlsort + " and ship=" + base.Request.Form["ship"] + " ";
            }
            if (base.Request.Form["pay"] != "0")
            {
                sqlsort = sqlsort + " and pay=" + base.Request.Form["pay"] + " ";
            }
            this.GridViewBind(sqlsort);
        }

        private void GridViewBind(string Sqlsort)
        {
            string sql = "select * from DB_order where languages=" + info.languages("") + " ";
            if (base.Request.Form["ordercode"] != "")
            {
                sql = sql + " and ordercode='" + base.Request.Form["ordercode"] + "' ";
            }
            if (base.Request.Form["states"] != null)
            {
                sql = sql + " and states=" + base.Request.Form["states"] + " ";
            }
            if (base.Request.Form["cname"] != "")
            {
                sql = sql + " and cname='" + base.Request.Form["cname"] + "' ";
            }
            if (base.Request.Form["cname1"] != "")
            {
                sql = sql + " and cname1='" + base.Request.Form["cname1"] + "' ";
            }
            if (base.Request.Form["address"] != "")
            {
                sql = sql + " and address1='" + base.Request.Form["address"] + "' ";
            }
            if (base.Request.Form["post"] != "")
            {
                sql = sql + " and post1='" + base.Request.Form["post"] + "' ";
            }
            if (base.Request.Form["tel"] != "")
            {
                sql = sql + " and tel1='" + base.Request.Form["tel"] + "' ";
            }
            if (base.Request.Form["phone"] != "")
            {
                sql = sql + " and phone1='" + base.Request.Form["phone"] + "' ";
            }
            if (base.Request.Form["ship"] != "0")
            {
                sql = sql + " and ship=" + base.Request.Form["ship"] + " ";
            }
            if (base.Request.Form["pay"] != "0")
            {
                sql = sql + " and pay=" + base.Request.Form["pay"] + " ";
            }
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
            this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
            this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
            this.LblRecordCount.Text = "总计 " + table.Rows.Count.ToString() + "个记录";
            if (table.Rows.Count == 0)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
                this.LblCurrentIndex.Visible = false;
                this.LblPageCount.Visible = false;
                this.LblRecordCount.Visible = false;
            }
            else if (this.GridView1.PageCount == 1)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
            }
            this.btnFirst.CommandName = "1";
            this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
            this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
            this.btnLast.CommandName = this.GridView1.PageCount.ToString();
            this.ddlCurrentPage.Items.Clear();
            if (this.GridView1.PageCount > 0)
            {
                for (int i = 1; i <= this.GridView1.PageCount; i++)
                {
                    this.ddlCurrentPage.Items.Add(i.ToString());
                }
                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            this.GridViewBind("");
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    if ((base.Request.Form["flag"].ToString() == "1") || (base.Request.Form["flag"].ToString() == "2"))
                    {
                        DataTable table = SQLAccess.GreatDs("select memberid,ordercode,orderscore,scoreprice,scoretype from DB_Order where orderid=" + this.GridView1.DataKeys[i].Value + "").Tables[0];
                        if ((table.Rows.Count > 0) && (table.Rows[0]["scoretype"].ToString() == "0"))
                        {
                            SQLAccess.DoSql(string.Concat(new object[] { "Update DB_Member set score1=score1+", Convert.ToInt32(table.Rows[0]["orderscore"]) - Convert.ToInt32(table.Rows[0]["scoreprice"]), ",score2=score2-", Convert.ToInt32(table.Rows[0]["scoreprice"]), ",score3=score3+", Convert.ToInt32(table.Rows[0]["orderscore"]), " where memberid=", table.Rows[0][0].ToString(), "" }));
                            SQLAccess.DoSql(string.Concat(new object[] { "insert into DB_Score (memberid,scorename,scoreprice,content,updatetime,scoretype) values (", table.Rows[0][0].ToString(), ",'订单赠积分：", table.Rows[0]["ordercode"], "','+", table.Rows[0]["orderscore"], "','赠积分：", table.Rows[0]["orderscore"], "','", DateTime.Now, "',0)" }));
                        }
                    }
                    SQLAccess.DoSql(string.Concat(new object[] { "Update DB_order set states=", base.Request.Form["flag"], ",scoretype=1 where orderid=", this.GridView1.DataKeys[i].Value, "" }));
                }
            }
            base.Response.Write("<script language=javascript>window.location.href='order.aspx';</script>");
            base.Response.End();
        }

        protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    SQLAccess.DoSql("delete from DB_order where orderid=" + this.GridView1.DataKeys[i].Value + "");
                    SQLAccess.DoSql("delete from DB_car where orderid=" + this.GridView1.DataKeys[i].Value + "");
                }
            }
            base.Response.Write("<script language=javascript>window.location.href='order.aspx';</script>");
            base.Response.End();
        }

        protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
        {
            string str = "0";
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    str = str + "," + this.GridView1.DataKeys[i].Value;
                }
            }
            base.Response.Write("<script language=javascript>window.location.href='order.aspx';window.open('ordera.aspx?oid=" + str + "');</script>");
            base.Response.End();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
         
            this.ImageButton5.Attributes["onclick"] = "return CheckSelect(true,'del');";
            this.ImageButton2.Attributes["onclick"] = "return CheckSelect(true,'update');";
            this.ImageButton6.Attributes["onclick"] = "return CheckSelect(true,'update');";
            if (!base.IsPostBack)
            {
                this.GridViewBind("");
                this.Bind();
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.GridViewBind("");
        }


    }
}